package com.smarthome.rpc.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import cn.jpush.api.JPushClient;
import cn.jpush.api.common.APIConnectionException;
import cn.jpush.api.common.APIRequestException;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;
import cn.jpush.api.report.ReceivedsResult;

import com.smarthome.rpc.PushMobileMessageService;
import com.smarthome.rpc.base.BaseRpcService;

@Service("pushMobileMessageService")
public class PushMobileMessageServiceImpl extends BaseRpcService implements PushMobileMessageService {

    private final static String masterSecret = "653391cbdef02105c7b482dc";
    private final static String appKey = "5eb716202803e161e9a44137";
    private static final Logger LOGGER = LoggerFactory.getLogger(PushMobileMessageServiceImpl.class);

    @Override
    public void pushMsgToSome(String content, String extraParam, String... accountIds) {
        PushPayload payload = PushPayload
                .newBuilder()
                .setPlatform(Platform.all())
                .setAudience(Audience.alias(accountIds))
                .setNotification(
                        Notification.newBuilder().addPlatformNotification(IosNotification.newBuilder().setSound("default").setBadge(5).setAlert(content).addExtra("extraParam", extraParam).build())
                                .addPlatformNotification(AndroidNotification.newBuilder().setAlert(content).setTitle("多啦家园").addExtra("extraParam", extraParam).build()).build())
                .setOptions(Options.newBuilder().setApnsProduction(true).build()).build();
        doPush(payload);
    }

    @Override
    public void pushMsgToAll(String content, String extraParam) {
        PushPayload payload = PushPayload
                .newBuilder()
                .setPlatform(Platform.all())
                .setAudience(Audience.all())
                .setNotification(
                        Notification.newBuilder().addPlatformNotification(IosNotification.newBuilder().setSound("default").setBadge(5).setAlert(content).addExtra("extraParam", extraParam).build())
                                .addPlatformNotification(AndroidNotification.newBuilder().setAlert(content).setTitle("多啦家园").addExtra("extraParam", extraParam).build()).build())
                .setOptions(Options.newBuilder().setApnsProduction(true).build()).build();
        doPush(payload);
    }

    private void doPush(PushPayload payload) {
        JPushClient jpushClient = new JPushClient(masterSecret, appKey, 3);
        try {
            PushResult result = jpushClient.sendPush(payload);
            System.out.println("Got result - " + result);
            LOGGER.info("Got result - " + result);
        } catch (APIConnectionException e) {
            e.printStackTrace();
            LOGGER.error("Connection error, should retry later", e);
        } catch (APIRequestException e) {
            e.printStackTrace();
            LOGGER.error("Should review the error, and fix the request", e);
            LOGGER.info("HTTP Status: " + e.getStatus());
            LOGGER.info("Error Code: " + e.getErrorCode());
            LOGGER.info("Error Message: " + e.getErrorMessage());
        }
    }

    private void statistics(String msgIds) {
        JPushClient jpushClient = new JPushClient(masterSecret, appKey);
        try {
            ReceivedsResult result = jpushClient.getReportReceiveds(msgIds);
            System.out.println("Got result - " + result);
            LOGGER.debug("Got result - " + result);
        } catch (APIConnectionException e) {
            e.printStackTrace();
            LOGGER.error("Connection error, should retry later", e);
        } catch (APIRequestException e) {
            e.printStackTrace();
            LOGGER.error("Should review the error, and fix the request", e);
            LOGGER.info("HTTP Status: " + e.getStatus());
            LOGGER.info("Error Code: " + e.getErrorCode());
            LOGGER.info("Error Message: " + e.getErrorMessage());
        }
    }

    public static void main(String[] args) {
//        new PushMobileMessageServiceImpl().pushMsgToAll("我是测试消息4", "3");
//         new PushMobileMessageServiceImpl().pushMsgToSome("我是测试1", "3", "33");
//         new PushMobileMessageServiceImpl().statistics("1589027892");
    }
}
